package com.serviceprovider.business.module.admin.shipping.services.dao.impl;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.shipping.model.ShipMode;
import com.serviceprovider.business.module.admin.shipping.services.dao.interfaces.IShipMode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Service;

//~--- JDK imports ------------------------------------------------------------

import java.math.BigDecimal;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("shipModeService")
public class ShipModeServiceDaoImpl extends JdbcDaoSupport implements IShipMode {
    private static final String F_ACTIVE = "active";

    // Constants for column names
    private static final String F_ID         = "ship_mode_id";
    private static final String F_SHIP_MODE  = "ship_mode";
    private static final String F_SHIP_PRICE = "ship_price";

    // SQL prepared statements
    private static final String INSERT_SHIP_MODE = new StringBuilder("INSERT INTO adm_ship_mode (").append(
                                                       F_SHIP_MODE).append(",").append(F_SHIP_PRICE).append(
                                                       ", ").append(F_ACTIVE).append(") VALUES (?, ?, ?)").toString();
    private static final String LIST_ALL_SHIP_MODE = "SELECT * FROM adm_ship_mode";
    private static final String UPDATE_SHIP_MODE   = new StringBuilder("UPDATE adm_ship_mode SET ").append(
                                                       F_SHIP_MODE).append(" = ?,").append(F_SHIP_PRICE).append(
                                                       " = ?,").append(F_ACTIVE).append(" = ? WHERE ").append(
                                                       F_ID).append(" = ?").toString();
    private static final String GET_SHIP_MODE =
        new StringBuilder("SELECT * FROM adm_ship_mode WHERE ").append(F_ID).append("=?").toString();
    private static final String DELETE_SHIP_MODE =
        new StringBuilder("DELETE FROM adm_ship_mode WHERE ").append(F_ID).append("=?").toString();

    @Autowired
    public ShipModeServiceDaoImpl(@Qualifier("jdbcTemplate") JdbcTemplate jdbcTemplate) {
        setJdbcTemplate(jdbcTemplate);
    }

    @Override
    public void create(ShipMode shipMode) {
        getJdbcTemplate().update(INSERT_SHIP_MODE, new Object[] { shipMode.getMode(), shipMode.getPrice(),
                shipMode.isActive() });
    }

    @Override
    public void delete(int id) {
        getJdbcTemplate().update(DELETE_SHIP_MODE, new Object[] { id });
    }

    @Override
    public ShipMode get(int id) {
        RowMapper mapper = new RowMapper() {
            @Override
            public ShipMode mapRow(ResultSet result, int rowNum) throws SQLException {
                ShipMode shipMode = new ShipMode();

                shipMode.setId(result.getInt(F_ID));
                shipMode.setMode(result.getString(F_SHIP_MODE));
                shipMode.setPrice(result.getInt(F_SHIP_PRICE));

                String activeModeStr = result.getString(F_ACTIVE);

                System.out.println("***********888activeModeStr=" + activeModeStr + " value="
                                   + activeModeStr.equals("1"));
                shipMode.setActive(activeModeStr.equals("1"));

                return shipMode;
            }
        };

        try {
            return (ShipMode) getJdbcTemplate().queryForObject(GET_SHIP_MODE, new Object[] { id }, mapper);
        } catch (EmptyResultDataAccessException ex) {
            return null;
        }
    }

    @Override
    public List<ShipMode> list() {
        List<ShipMode> listShip = new ArrayList<ShipMode>();
        List           list     = getJdbcTemplate().queryForList(LIST_ALL_SHIP_MODE);
        Map            row;
        ShipMode       ship;

        for (Object obj : list) {
            ship = new ShipMode();
            row  = (Map) obj;
            ship.setId(((Long) row.get(F_ID)).intValue());
            ship.setMode(row.get(F_SHIP_MODE).toString());

            // ship.setPrice(Float.parseFloat(row.get(F_SHIP_PRICE).toString()));
            ship.setPrice(((BigDecimal) row.get(F_SHIP_PRICE)).intValue());

            // int active = Integer.parseInt(row.get(F_SHIP_PRICE).toString());
            String activeModeStr = row.get(F_ACTIVE).toString();

            System.out.println("***********888activeModeStr=" + activeModeStr + " value=" + activeModeStr.equals("1"));
            ship.setActive(activeModeStr.equals("1"));

            // ship.setActive(active == 1 ? true : false);
            listShip.add(ship);
        }

        return listShip;
    }

    @Override
    public void modify(ShipMode shipMode) {
        getJdbcTemplate().update(UPDATE_SHIP_MODE, new Object[] { shipMode.getMode(), shipMode.getPrice(),
                shipMode.isActive(), shipMode.getId() });
    }
}



